package com.stx.jeepay.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.stx.jeepay.core.dao.TransferOrderDao;
import com.stx.jeepay.core.entity.TransferOrderEntity;
import com.stx.jeepay.core.service.TransferOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("transferOrderService")
public class TransferOrderServiceImpl extends ServiceImpl<TransferOrderDao, TransferOrderEntity> implements TransferOrderService {

    /**
     * 查询商户订单
     **/
    @Override
    public TransferOrderEntity queryMchOrder(String mchNo, String mchOrderNo, String transferId) {
        if (StringUtils.isNotEmpty(transferId)) {
            return getOne(new LambdaQueryWrapper<TransferOrderEntity>()
                    .eq(TransferOrderEntity::getMchNo, mchNo)
                    .eq(TransferOrderEntity::getTransferId, transferId));
        } else if (StringUtils.isNotEmpty(mchOrderNo)) {
            return getOne(new LambdaQueryWrapper<TransferOrderEntity>()
                    .eq(TransferOrderEntity::getMchNo, mchNo)
                    .eq(TransferOrderEntity::getMchOrderNo, mchOrderNo));
        } else {
            return null;
        }
    }

    /**
     * 更新转账订单状态  【转账订单生成】 --》 【转账中】
     **/
    @Override
    public boolean updateInit2Ing(String transferId) {
        TransferOrderEntity updateOrder = new TransferOrderEntity();
        updateOrder.setState(TransferOrderEntity.STATE_ING);

        return update(updateOrder, new LambdaQueryWrapper<TransferOrderEntity>()
                .eq(TransferOrderEntity::getTransferId, transferId)
                .eq(TransferOrderEntity::getState, TransferOrderEntity.STATE_INIT));
    }

    /**
     * 更新转账订单状态  【转账中】 --》 【转账成功/转账失败】
     **/
    @Override
    public boolean updateIng2SuccessOrFail(String transferId, Byte updateState, String channelOrderNo, String channelErrCode, String channelErrMsg) {
        if (updateState == TransferOrderEntity.STATE_ING) {
            return true;
        } else if (updateState == TransferOrderEntity.STATE_SUCCESS) {
            return updateIng2Success(transferId, channelOrderNo);
        } else if (updateState == TransferOrderEntity.STATE_FAIL) {
            return updateIng2Fail(transferId, channelOrderNo, channelErrCode, channelErrMsg);
        }
        return false;
    }

    /**
     * 更新转账订单状态  【转账中】 --》 【转账失败】
     **/
    @Transactional
    public boolean updateIng2Fail(String transferId, String channelOrderNo, String channelErrCode, String channelErrMsg) {
        TransferOrderEntity updateOrder = new TransferOrderEntity();
        updateOrder.setState(TransferOrderEntity.STATE_FAIL);
        updateOrder.setErrCode(channelErrCode);
        updateOrder.setErrMsg(channelErrMsg);
        updateOrder.setChannelOrderNo(channelOrderNo);

        return update(updateOrder, new LambdaQueryWrapper<TransferOrderEntity>()
                .eq(TransferOrderEntity::getTransferId, transferId)
                .eq(TransferOrderEntity::getState, TransferOrderEntity.STATE_ING));
    }

    @Override
    public IPage<TransferOrderEntity> pageList(IPage iPage, LambdaQueryWrapper<TransferOrderEntity> wrapper, TransferOrderEntity transferOrder, JSONObject paramJSON) {

        if (StringUtils.isNotEmpty(transferOrder.getTransferId())) {
            wrapper.eq(TransferOrderEntity::getTransferId, transferOrder.getTransferId());
        }
        if (StringUtils.isNotEmpty(transferOrder.getMchOrderNo())) {
            wrapper.eq(TransferOrderEntity::getMchOrderNo, transferOrder.getMchOrderNo());
        }
        if (StringUtils.isNotEmpty(transferOrder.getChannelOrderNo())) {
            wrapper.eq(TransferOrderEntity::getChannelOrderNo, transferOrder.getChannelOrderNo());
        }
        if (StringUtils.isNotEmpty(transferOrder.getMchNo())) {
            wrapper.eq(TransferOrderEntity::getMchNo, transferOrder.getMchNo());
        }
        if (transferOrder.getState() != null) {
            wrapper.eq(TransferOrderEntity::getState, transferOrder.getState());
        }
        if (StringUtils.isNotEmpty(transferOrder.getAppId())) {
            wrapper.eq(TransferOrderEntity::getAppId, transferOrder.getAppId());
        }
        if (paramJSON != null) {
            if (StringUtils.isNotEmpty(paramJSON.getString("createdStart"))) {
                wrapper.ge(TransferOrderEntity::getCreatedAt, paramJSON.getString("createdStart"));
            }
            if (StringUtils.isNotEmpty(paramJSON.getString("createdEnd"))) {
                wrapper.le(TransferOrderEntity::getCreatedAt, paramJSON.getString("createdEnd"));
            }
        }
        // 三合一订单
        if (paramJSON != null && StringUtils.isNotEmpty(paramJSON.getString("unionOrderId"))) {
            wrapper.and(wr -> {
                wr.eq(TransferOrderEntity::getTransferId, paramJSON.getString("unionOrderId"))
                        .or().eq(TransferOrderEntity::getMchOrderNo, paramJSON.getString("unionOrderId"))
                        .or().eq(TransferOrderEntity::getChannelOrderNo, paramJSON.getString("unionOrderId"));
            });
        }
        wrapper.orderByDesc(TransferOrderEntity::getCreatedAt);

        return page(iPage, wrapper);
    }

    /**
     * 更新转账订单状态  【转账中】 --》 【转账成功】
     **/
    @Transactional
    public boolean updateIng2Success(String transferId, String channelOrderNo) {
        TransferOrderEntity updateOrder = new TransferOrderEntity();
        updateOrder.setState(TransferOrderEntity.STATE_SUCCESS);
        updateOrder.setChannelOrderNo(channelOrderNo);
        updateOrder.setSuccessTime(new Date());

        // 更新订单数据
        boolean update = update(updateOrder, new LambdaUpdateWrapper<TransferOrderEntity>()
                .eq(TransferOrderEntity::getTransferId, transferId)
                .eq(TransferOrderEntity::getState, TransferOrderEntity.STATE_ING));
        if (!update) {
            return false;
        }

        return true;
    }
}